/*****************************************************************************
 * Copyright (c) 2010 -, Anders All rights reserved
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact: Anders Uddenberg (anders.uddenberg@gmail.com)
 * 
 *
 ****************************************************************************/
package org.easypr.organizer.client.presenter;

import java.util.ArrayList;

import org.easypr.organizer.client.BackboneServiceAsync;
import org.easypr.organizer.client.event.DisplayInformationEvent;
import org.easypr.organizer.shared.MyContact;
import org.easypr.organizer.shared.MyUser;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

// TODO: Auto-generated Javadoc
/**
 * The Class BrowsePresenter.
 */
public class BrowsePresenter implements Presenter {

	/**
	 * Define the interface the view needs to implement.
	 * 
	 * @author Anders
	 */
	public interface Display {
		
		/**
		 * Sets the data.
		 * 
		 */
		void setData();

		/**
		 * Sets the contacts.
		 *
		 * @param contacts the new contacts
		 */
		void setContacts(ArrayList<MyContact> contacts);
		
		/**
		 * As widget.
		 * 
		 * @return the widget
		 */
		Widget asWidget();
		
		/**
		 * Gets the previous link.
		 *
		 * @return the previous link
		 */
		Anchor getPreviousLink();
		
		/**
		 * Gets the next link.
		 *
		 * @return the next link
		 */
		Anchor getNextLink();
		
		/**
		 * Increase page.
		 */
		void increasePage();
		
		/**
		 * Decrease page.
		 */
		void decreasePage();
		
		/**
		 * Gets the content table.
		 *
		 * @return the content table
		 */
		FlexTable getContentTable();
	}

	/** The event bus. */
	private final HandlerManager eventBus;
	
	/** The display. */
	private final Display display;
	
	/** The parts. */
	private String[] parts;

	/** The backbone rpc. */
	BackboneServiceAsync backboneRPC;	
	
	/** The user. */
	MyUser user;
	
	/**
	 * Instantiates a new menu presenter.
	 *
	 * @param backboneRPC the backbone rpc
	 * @param eventBus the event bus
	 * @param view the view
	 * @param user the user
	 * @param parts the parts
	 */
	public BrowsePresenter(BackboneServiceAsync backboneRPC, HandlerManager eventBus, Display view, MyUser user, String[] parts) {
		this.backboneRPC = backboneRPC;
		this.eventBus = eventBus;
		this.display = view;
		this.user = user;
		this.parts = parts;
	}
	

	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.Presenter#go(com.google.gwt.user.client.ui.HasWidgets)
	 */
	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
		bind();
		updateContent();
	}
	
	/**
	 * Bind.
	 */
	private void bind() {
		/*
		display.getContentTable().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Cell cell = display.getContentTable().getCellForEvent(event);
				long contactid = Long.parseLong(cell.getElement().getId());
				//Window.alert(cell.getElement().getId());
				//History.newItem("contact/");
				
			}
		});
		*/
		
		display.getPreviousLink().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				doDecreasePage();
			}
		});
		
		display.getNextLink().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				doIncreasePage();
			}
		});
	}
	
	/**
	 * Do decrease page.
	 */
	private void doDecreasePage() {
		display.decreasePage();
		display.setData();
	}
	
	/**
	 * Do increase page.
	 */
	private void doIncreasePage() {
		display.increasePage();
		display.setData();
	}
	
	/**
	 * Main function to update the content of the container.
	 */
	public void updateContent() {
		
		AsyncCallback<ArrayList<MyContact>> callback = new AsyncCallback<ArrayList<MyContact>>() {
			public void onFailure(Throwable caught) {
				eventBus.fireEvent(new DisplayInformationEvent("Unable to proceed with the request!", DisplayInformationEvent.Severity.FAILURE));
			}

			public void onSuccess(ArrayList<MyContact> contacts) {
				display.setContacts(contacts);
				display.setData();
			}
		};
		
		
		// Fetch the contacts
		if(this.parts.length > 1) {
			// We have an active filter
			backboneRPC.fetchContacts(this.user, this.parts[1], callback);
		} else {
			// Just fetch all
			backboneRPC.fetchContacts(this.user, null, callback);
		}
		
	}
}

